package com.example.demo.controller;

import com.example.demo.model.Dog;
import com.example.demo.service.DogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//初始url: http://localhost:8080/dog/show
@Controller
@RequestMapping(value="/dog")
public class DogController {
    //字段注入
   @Autowired
   private DogService dogService;

    //构造函数注入，不可变性、安全性，spring官方推荐
//    private final DogService dogService;
//    @Autowired
//    public DogController(DogService dogService)
//    {
//        this.dogService=dogService;
//    }

    //setter方式注入
//    private DogService dogService;
//    @Autowired
//    public void setDogService(DogService dogService) {
//        this.dogService = dogService;
//    }

    //   返回的不是字符串，而是dog.html
    @RequestMapping(value = "/show")
    public String show(){
        //返回dog页面显示内容
        return "dog";
    }

    /**
     * 加上@ResponseBody会返回 dog 字符串
     * @return
     */
    @RequestMapping(value = "/show2")
    @ResponseBody
    public String show2(){
        //返回user页面显示内容
        return "dog";
    }


   @RequestMapping(value = "/getDog")
   public String selectByID(Integer id, Model model){

       Dog dog= dogService.selectByID(id);
       model.addAttribute("dog",dog);
       if(dog!=null)
       {
           return "getDog";
       }
       else
       {
           return "error";
       }
   }

   @GetMapping("/getDogInfo1")
   @ResponseBody
   public Object getDogInfo1(@RequestParam Integer id)
   {
       return dogService.selectByID(id);
   }

    /**
     * 不加@ResponseBody会报错
     * template might not exist or might not be accessible by any of the configured Template Resolvers
     * @param id
     * @return
     */
    @GetMapping("/getDogInfo2")
    public Object getDogInfo2(@RequestParam Integer id)
    {
        return dogService.selectByID(id);
    }

    @PostMapping("/addDog")
    @ResponseBody
    public Object addDog(@RequestBody Dog dog)
    {
        return dogService.addDog(dog);
    }
}
